<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>新闻列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="inputForm" ref="inputForm" label-width="80px" class="demo-ruleForm">
      <!--      <el-form-item label="查询条件">-->
      <!--        <el-select v-model="ruleForm.items" placeholder="请选择">-->
      <!--          <el-option v-for="item in itemsOptions"-->
      <!--                     :key="item.id"-->
      <!--                     :label="item.name"-->
      <!--                     :value="item.name">-->
      <!--          </el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="投稿标题">
        <el-input v-model="inputForm.title" placeholder="请输入投稿标题" clearable></el-input>
      </el-form-item>
      <el-form-item label="投稿作者">
        <el-input v-model="inputForm.author" placeholder="请输入投稿作者" clearable></el-input>
      </el-form-item>
      <el-form-item label="投稿栏目">
        <el-input v-model="inputForm.submissionColumn" placeholder="请输入投稿栏目" clearable></el-input>
      </el-form-item>
      <el-form-item label="投稿部门">
        <el-input v-model="inputForm.submissionDepartment" placeholder="请输入投稿部门" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('inputForm')">查 询</el-button>
        <el-button @click="resetForm('inputForm')">重 置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="title" label="投稿标题" width="150" align="center"></el-table-column>
      <el-table-column prop="author" label="投稿作者" width="100" align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="contactNumber" label="联系电话" width="130" align="center"></el-table-column>
      <el-table-column prop="submissionColumn" label="投稿栏目" width="80" align="center"></el-table-column>
      <el-table-column prop="submissionDepartment" label="投稿部门" width="80" align="center"></el-table-column>
      <el-table-column prop="content" label="投稿内容" align="center"></el-table-column>
      <el-table-column prop="checkUser" label="审核人" width="100" align="center"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑新闻的对话框 -->
    <el-dialog title="修改新闻" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="投稿标题" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="投稿作者" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.author"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.contactNumber"></el-input>
        </el-form-item>
        <el-form-item label="投稿栏目" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.submissionColumn"></el-input>
        </el-form-item>
        <el-form-item label="投稿部门" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.submissionDepartment"></el-input>
        </el-form-item>
        <el-form-item label="投稿内容" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="ruleForm.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // itemsOptions: [
      //   {id: '1', name: '投稿标题'},
      //   {id: '2', name: '投稿作者'},
      //   {id: '3', name: '投稿栏目'},
      //   {id: '4', name: '投稿部门'}],
      inputForm:{
        title: '',
        author: '',
        submissionColumn: '',
        submissionDepartment: '',
      },
      tableData: [],
      dialogFormVisible: false,
      ruleForm: {
        id: '',
        title: '',
        author: '',
        contactNumber: '',
        submissionColumn: '',
        submissionDepartment: '',
        checkUser: '',
        content: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {  /*对表单进行检查验证*/
        if (valid) {
          if (this.ruleForm.title=this.inputForm.title) {
            this.loadNewsListByTitle();
          }
          if (this.ruleForm.author=this.inputForm.author) {
            this.loadNewsListByAuthor();
          }
          if (this.ruleForm.submissionColumn=this.inputForm.submissionColumn) {
            this.loadNewsListBySubmissionColumn();
          }
          if (this.ruleForm.submissionDepartment=this.inputForm.submissionDepartment) {
            this.loadNewsListBySubmissionDepartment();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      location.reload()
    },
    submitEdit() {
      let url = 'http://localhost:9800/news/' + this.ruleForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);
      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改新闻成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadNewsList();
        } else if (responseBody.state == 40900) {
          this.$message.error(responseBody.message);
        } else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible = false;
          this.loadNewsList();
        }
      });
    },
    handleEdit(news) {
      let message = '您正在尝试编辑【' + news.id + '-' + news.title + '】的新闻详情……';
      console.log(message);
      let url = 'http://localhost:9800/news/' + news.id + '/id';
      console.log(url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.ruleForm = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$message.error(responseBody.message);
          this.loadNewsList();
        }
      });
    },
    openDeleteConfirm(news) {
      let message = '此操作将永久删除【' + news.title + '】新闻，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(news);
      }).catch(() => {
      });
    },
    handleDelete(news) {
      let url = 'http://localhost:9800/news/' + news.id + '/delete';
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 20000 || responseBody.state == 40400) {
          this.loadNewsList();
        }
      });
    },
    loadNewsList() {
      console.log('loadNewsList');
      let url = 'http://localhost:9800/news';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
    loadNewsListByTitle() {
      console.log('loadNewsListByTitle');
      let url = 'http://localhost:9800/news/' + this.ruleForm.title + '/listByTitle';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
    loadNewsListByAuthor() {
      console.log('loadNewsListByAuthor');
      let url = 'http://localhost:9800/news/' + this.ruleForm.author + '/listByAuthor';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
    loadNewsListBySubmissionDepartment() {
      console.log('loadNewsListBySubmissionDepartment');
      let url = 'http://localhost:9800/news/' + this.ruleForm.submissionDepartment + '/listBySubmissionDepartment';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
    loadNewsListBySubmissionColumn() {
      console.log('loadNewsListBySubmissionColumn');
      let url = 'http://localhost:9800/news/' + this.ruleForm.submissionColumn + '/listBySubmissionColumn';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
  },
  mounted() {
    console.log('mounted');
    this.loadNewsList();
  }
}
</script>